#ifndef DB_MANAGER_H
#define DB_MANAGER_H

#include "common.h"
#include <sqlite3.h>

typedef struct {
    sqlite3 *db;
    char *db_path;
    pthread_mutex_t mutex;
    bool initialized;
} db_manager_t;

// 数据库管理API
int db_manager_init(db_manager_t *db_mgr, const char *db_path);
int db_manager_close(db_manager_t *db_mgr);
int db_manager_execute(db_manager_t *db_mgr, const char *sql,
                       int (*callback)(void*, int, char**, char**),
                       void *data);
int db_manager_execute_transaction(db_manager_t *db_mgr,
                                   int (*transaction_fn)(sqlite3*, void*),
                                   void *data);

// 配置表操作
int db_config_set(db_manager_t *db_mgr, const char *key, const char *value);
char *db_config_get(db_manager_t *db_mgr, const char *key);
int db_config_get_int(db_manager_t *db_mgr, const char *key, int default_value);
json_t *db_config_get_json(db_manager_t *db_mgr, const char *key);

// 数据池表操作
int db_datapool_set(db_manager_t *db_mgr, const char *key, json_t *value);
json_t *db_datapool_get(db_manager_t *db_mgr, const char *key);
int db_datapool_delete(db_manager_t *db_mgr, const char *key);
int db_datapool_get_all(db_manager_t *db_mgr,
                        int (*callback)(void*, const char*, json_t*),
                        void *data);

// 事件日志表操作
int db_event_log(db_manager_t *db_mgr, event_type_t type, const char *source,
                 const char *description, json_t *data);

// 系统状态表操作
int db_system_status_set(db_manager_t *db_mgr, const char *component,
                         const char *status, json_t *details);
json_t *db_system_status_get(db_manager_t *db_mgr, const char *component);

#endif
